//
//  YMFeedbackAddPhotoCell.m
//  SmartCar
//
//  Created by y s on 2022/11/18.
//

#import "YMFeedbackAddPhotoCell.h"
#import "YMBaseMenuFlowLayout.h"

#define k_ItemHeight [SFitUtil fit:20+75]
#define k_MaxNum 4
@interface YMFeedbackAddPhotoCell ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UIView *vBg;
@property (nonatomic, strong) UILabel *lblTitle;
@property (nonatomic, strong) UILabel *lblSub;
@property (nonatomic, strong) UICollectionView *cvMain;


@end

@implementation YMFeedbackAddPhotoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark - UI methods
- (void)setUI{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = [UIColor clearColor];
    self.backgroundColor = [UIColor clearColor];
    
    [self.contentView addSubview:self.vBg];
    [self.vBg addSubview:self.lblTitle];
    [self.vBg addSubview:self.lblSub];
    [self.vBg addSubview:self.cvMain];
    
    [self.vBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.top.bottom.mas_equalTo(0);
    }];
    [self.lblTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.top.mas_equalTo([SFitUtil fit:14]);
    }];
    [self.lblSub mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.lblTitle);
        make.top.equalTo(self.lblTitle.mas_bottom).offset([SFitUtil fit:12]);
    }];
    [self.cvMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:10]);
        make.right.mas_equalTo([SFitUtil fit:-10]);
        make.top.mas_equalTo([SFitUtil fit:62]);
        make.bottom.mas_equalTo([SFitUtil fit:-18]);
    }];
}


#pragma mark - public methods
+ (CGFloat)getCellHeight:(NSInteger)count{
    CGFloat cellH = [SFitUtil fit:62+18];
    cellH += [YMFeedbackAddPhotoCell getItemViewHeight:count];
    return cellH;
}
+ (CGFloat)getItemViewHeight:(NSInteger)count{
    CGFloat itemH = 0;
    if (count < k_MaxNum){
        NSInteger numD = (count+1)%3;
        NSInteger line = (count+1)/3;
        if (numD > 0){
            line = line+1;
        }
        itemH += k_ItemHeight*line;
    }else{
        NSInteger numD = k_MaxNum%3;
        NSInteger line = k_MaxNum/3;
        if (numD > 0){
            line = line+1;
        }
        itemH += k_ItemHeight*line;
    }
    return itemH;
}
- (void)setArrPhoto:(NSArray *)arrPhoto{
    _arrPhoto = arrPhoto;
    self.lblTitle.text = [NSString stringWithFormat:@"%@(%ld/4)",KLanguage(@"image_txt"),arrPhoto.count];
    [self.cvMain reloadData];
}

#pragma mark - Event Action

#pragma mark -- UICollectionView Delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.arrPhoto.count+1 > k_MaxNum){
        return k_MaxNum;
    }else{
        return self.arrPhoto.count+1;
    }
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    WEAK_SELF;
    YMFeedbackAddPhotoItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YMFeedbackAddPhotoItemCell" forIndexPath:indexPath];
    if (self.arrPhoto.count == k_MaxNum){
        cell.showAdd = NO;
    }else{
        if (indexPath.item < self.arrPhoto.count){
            cell.showAdd = NO;
        }else{
            cell.showAdd = YES;
        }
    }
    cell.indexItem = indexPath.item;
    if (indexPath.item < self.arrPhoto.count){
        cell.strImage = self.arrPhoto[indexPath.item];
    }
    cell.closeClickBlock = ^(NSInteger indexItem) {
        if (weakSelf.deletePhotBlock){
            weakSelf.deletePhotBlock(indexItem);
        }
    };
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.arrPhoto.count == k_MaxNum){
        return;
    }else{
        if (indexPath.item < self.arrPhoto.count){
            return;
        }else{
            if (self.addPhotBlock){
                self.addPhotBlock();
            }
        }
    }
}


#pragma mark -- setter and getter methods
- (UIView *)vBg{
    if (_vBg == nil) {
        _vBg = [[UIView alloc] init];
        _vBg.backgroundColor = STheme.D9;
        _vBg.clipsToBounds = YES;
        _vBg.layer.cornerRadius = [SFitUtil fit:8];
    }
    return _vBg;
}
- (UILabel *)lblTitle{
    if (_lblTitle == nil){
        _lblTitle = [SThemeUtil boldLabel:@"图片(0/4)" color:STheme.H_D1 fontSize:16];
    }
    return _lblTitle;
}
- (UILabel *)lblSub{
    if (_lblSub == nil){
        _lblSub = [SThemeUtil label:KLanguage(@"question_hint_txt") color:STheme.H_D4 fontSize:14];
    }
    return _lblSub;
}
- (UICollectionView *)cvMain{
    if (_cvMain == nil) {
        CGFloat itemW = k_ItemHeight;
        CGFloat itemH = k_ItemHeight;
        YMBaseMenuFlowLayout *layout = [[YMBaseMenuFlowLayout alloc] init];
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.itemSize = CGSizeMake(itemW, itemH);
        layout.minimumInteritemSpacing = CGFLOAT_MIN;
        layout.minimumLineSpacing = CGFLOAT_MIN;
        layout.maxSpace = (SCREEN_WIDTH-[SFitUtil fit:15]*2-[SFitUtil fit:10]*2)*0.5-1;
        _cvMain = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _cvMain.delegate = self;
        _cvMain.dataSource = self;
        _cvMain.scrollEnabled = NO;
        _cvMain.showsVerticalScrollIndicator = NO;
        _cvMain.showsHorizontalScrollIndicator = NO;
        _cvMain.backgroundColor = [UIColor clearColor];
        [_cvMain registerClass:[YMFeedbackAddPhotoItemCell class] forCellWithReuseIdentifier:@"YMFeedbackAddPhotoItemCell"];
    }
    return _cvMain;
}


@end


@interface YMFeedbackAddPhotoItemCell ()

@property (nonatomic, strong) UIImageView *imgvIcon;
@property (nonatomic, strong) UIButton *btnClose;

@property (nonatomic, strong) UIImageView *imgvAdd;

@end

@implementation YMFeedbackAddPhotoItemCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{
    [self.contentView addSubview:self.imgvIcon];
    [self.contentView addSubview:self.imgvAdd];
    [self.contentView addSubview:self.btnClose];
    
    [self.imgvIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([SFitUtil fit:20]);
        make.left.mas_equalTo([SFitUtil fit:10]);
        make.right.mas_equalTo([SFitUtil fit:-10]);
        make.bottom.mas_equalTo(0);
    }];
    [self.imgvAdd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.imgvIcon);
        make.size.mas_equalTo([SFitUtil fitSize:75 height:75]);
    }];
    [self.btnClose mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.top.equalTo(self.imgvIcon).offset([SFitUtil fit:-15]);
        make.size.mas_equalTo([SFitUtil fitSize:20 height:30]);
    }];
    
}

- (void)setShowAdd:(BOOL)showAdd{
    self.imgvAdd.hidden = !showAdd;
    self.btnClose.hidden = showAdd;
    if (showAdd){
        self.imgvIcon.image = [UIImage imageNamed:@""];
    }
}
- (void)setStrImage:(NSString *)strImage{
    _strImage = strImage;
    [SUIUtil setImage:self.imgvIcon url:strImage];
}


- (void)closeClickAction{
    if (self.closeClickBlock){
        self.closeClickBlock(self.indexItem);
    }
}

#pragma mark -- setter and getter
- (UIImageView *)imgvIcon{
    if (_imgvIcon == nil){
        _imgvIcon = [[UIImageView alloc] init];
        _imgvIcon.clipsToBounds = YES;
        _imgvIcon.layer.cornerRadius = [SFitUtil fit:3];
        _imgvIcon.backgroundColor = [SUIUtil colorOf:0xF5F5F5];
    }
    return _imgvIcon;
}
- (UIButton *)btnClose{
    if (_btnClose == nil){
        _btnClose = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnClose setImage:[UIImage imageNamed:@"tools_close_icon_16x16"] forState:UIControlStateNormal];
        [_btnClose addTarget:self action:@selector(closeClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnClose;
}
- (UIImageView *)imgvAdd{
    if (_imgvAdd == nil){
        _imgvAdd = [[UIImageView alloc] init];
        _imgvAdd.image = [UIImage imageNamed:@"mine_feed_back_add_image"];
    }
    return _imgvAdd;
}



@end
